const mongoose = require('mongoose');
const News = require('../models/news.model');
const async = require('async');
const axios = require('axios');
exports.create = function (req, res, next) {
    const news = new News(req.body);
    news.save().then(data => {
        res.json(data);
    });
}

exports.update = function (req, res, next) {
    var id = req.params.id;
    // req.query
    // req.body
    // req.params
    News.findByIdAndUpdate(id, { $set: req.body }, { new: false }).then(data => {
        res.json(data); // 修改之前的数据
    })
}

exports.get = function (req, res, next) {
    var id = req.params.id;
    News.findById(id).then(data => {
        res.json(data);
    })
}

exports.gettitle = function (req, res, next) {
    var title = req.params.title;
    News.find({ title: new RegExp(title, 'g') }).then(data => {
        res.json(data);
        console.log(data)
    })
}

exports.remove = function (req, res, next) {
    const id = req.params.id;
    News.findByIdAndRemove(id).then(data => {
        res.json(data);
    })
}

exports.removes = function (req, res, next) {
    var ids = req.body.ids.split(',');
    News.remove({ _id: { $in: ids } }).then(() => {
        res.json({
            "message": "删除多条新闻成功", status: 200
        });
    })
}

exports.list = function (req, res, next) {
    // if(!req.params.cateId){
    //     var type = req.params.type;
    //     News.find({type:type}).then(data=>{
    //         //console.log(data);
    //         res.json(data);
    //     })
    // }else if(req.params.cateId && req.params.cateId.length>0){
    // var cateId = req.params.cateId;

    // console.log(cateId);
    // News.find({cateId:cateId}).then(data=>{
    //     res.json(data);
    // })
    // }
    var type = req.params.type;
    News.find({ type: type }).then(data => {
        //console.log(data);
        var c=JSON.parse(JSON.stringify(data));
        var arr = [];
        for (let prop of c) {
            arr.push(function axiosfn(callback) {
                axios.get('http://localhost:3000/comment/replynum/' + prop._id).then((result) => {
                    prop.reply = result.data;
                    callback();
                });
            })
        }
        async.parallel(arr,function(err){
            console.log(err)
            console.log(22);
            res.json(c)
        })
    })
}


exports.lists = function (req, res, next) {
    if (req.params.cateId && req.params.cateId.length > 0) {
        var cateId = req.params.cateId;
        News.find({ cateId: cateId }).then(data => {
            // var a = data;
            // var b = JSON.stringify(a);
            // var c = JSON.parse(b);
            var c=JSON.parse(JSON.stringify(data));
            var arr = [];
            for (let prop of c) {
                arr.push(function axiosfn(callback) {
                    axios.get('http://localhost:3000/comment/replynum/' + prop._id).then((result) => {
                        prop.reply = result.data;
                        callback();
                    });
                })
            }
            async.parallel(arr,function(err){
                console.log(err)
                console.log(22);
                res.json(c)
            })

           
        })

    }
}


